package com.zecan.blog.service.impl;

import com.zecan.blog.common.status.CommentStatus;
import com.zecan.blog.common.status.UserStatus;
import com.zecan.blog.controller.exception.CommentException;
import com.zecan.blog.controller.exception.UserException;
import com.zecan.blog.dao.ArticleMapper;
import com.zecan.blog.dao.CommentMapper;
import com.zecan.blog.dao.converter.BlogCommentConverter;
import com.zecan.blog.dto.comment.ReplyCommentDto;
import com.zecan.blog.entity.Comment;
import com.zecan.blog.service.CommentService;
import com.zecan.blog.utils.StringUtils;
import com.zecan.blog.vo.comment.CommentVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Stream;

/**
 * @ClassName CommentServiceImpl
 * @Description
 * @date 2022/11/11 11:34
 * @Version 1.0
 */
@Service
public class CommentServiceImpl implements CommentService {


    @Resource
    private CommentMapper commentMapper;

    @Resource
    private ArticleMapper articleMapper;

    @Override
    public List<CommentVo> getArticleCommentList(Integer articleId) {
        // 判定当前文章是否存在
        if(articleMapper.checkArticleIsExist(articleId).equals(0)) {
            throw new UserException(UserStatus.USER_ARTICLE_IS_NOT_EXIST);
        }

        // 获取评论信息
        List<Comment> commentList = commentMapper.findCommentListByArticleId(articleId);
        if(Objects.isNull(commentList)) {
           return null; // 如果当前不存在评论信息 直接返回null
        }
        // 查询所有子级回复
        List<Comment> childrenList =
                commentMapper.findChildrenListByArticleIdAndParentId(articleId);
        List<CommentVo> commentVoList = new ArrayList<>();

        // 数据整合
        for (Comment comment : commentList) {
            commentVoList.add( CommentVo.buildCommentVo(comment,childrenList));
        }

        return commentVoList;
    }

    @Override
    public void sendComment(ReplyCommentDto replyCommentDto) {
        // 查看是否有父级 , 不存在父级自身为 一级 值为-1
        Integer commentParentId = replyCommentDto.getCommentParentId();

        // 评论状态判定
        if(commentParentId != -1) {
            // 如果 不等于-1 说明存在父级, 获取父级的状态判定.
            Integer commentIsExist = commentMapper.findCommentIsExist(commentParentId);
            // 如果此时已经被删除 直接返回父级不存在
            if(commentIsExist.equals(0)) {
                throw  new CommentException(CommentStatus.COMMENT_IS_EXIST);
            }
        }
        // dto -> entity
        Comment comment = BlogCommentConverter.INSTANCE.commentConverterEntity(replyCommentDto);
        comment.setCommentCreateTime(StringUtils.dateFormat(new Date()));

        // 添加评论
        Integer row = commentMapper.insertCommentOrReply(comment);
        if( row != 1) {
            throw  new CommentException(CommentStatus.COMMENT_SEND_ERR);
        }
        // 添加评论 or 回复成功
    }

    @Override
    public void delComment(Integer commentId) {
      commentMapper.updateCommentStatus(commentId);
    }

    @Override
    public List<CommentVo> getCommentList(Integer offset ,Integer limit) {
        List<CommentVo> list = new ArrayList<>();
        List<Comment> comments = commentMapper.selectCommentList(offset,limit);
        if(comments != null){
            comments.forEach(item -> {
                list.add(CommentVo.buildCommentVo(item));
            });
        }
        return list;
    }
}
